package com.peanut.order.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.peanut.common.pojo.Car;
import com.peanut.common.pojo.Order;
import com.peanut.common.vo.SysResult;
import com.peanut.order.service.OrderService;

@RestController
@RequestMapping("order/manage")
public class OrderController {
	@Autowired
	private OrderService peanutService;
	//查询订单
	@RequestMapping("queryorder")
	public List<Order> SelectOrder(String userId){
		return peanutService.queryOrder(userId);
	}
	//查询单个订单
	@RequestMapping("queryoneorder")
	public Order selectoneOrder(String orderId){
		Order order=peanutService.queryoneOrder(orderId);
		return order;
		
		
		
	}
	//查询订单详情
	@RequestMapping("querycar")
	public Car selectCar(String carId){
		
			Car car =peanutService.selectByCarId(carId);
			return car;
		
	}
	//不足三日提醒(需要参数 orderId)
	@RequestMapping("reminder")
	public SysResult Reminder(Order order){
		try{
			
			Integer result=peanutService.ResidualReminder(order);
			if(result==0){
				return SysResult.ok();
			}else{
				return SysResult.build(201, "您的租期不足三天，请及时续费", null);
			}
			
		}catch (Exception e) {
			e.getStackTrace();
			return SysResult.build(202, "错误请重试", null);
		}
	}
	//续租(需要参数orderId time)
	@RequestMapping("renewal")
	public SysResult addTime(Order order,Integer time) {
		try {
			Integer result=peanutService.addTime(time,order);
			if(result==0){
				return SysResult.build(201, "您租车时间已超,请付清超时费用", null);
			}else{
				return SysResult.ok();
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			return SysResult.build(202, "错误请重试", null);
		}
		
	}
	//还车(需要参数 orderId carId)
	@RequestMapping("returncar")
	public SysResult feeincrease(Order order){
		try{
			Integer state =peanutService.returncar(order);
			if(state==1){
				return SysResult.build(201,"您未按规定日期还车已经自动扣除超时费用",null);
			}
			else{
				return SysResult.build(200, "还车成功", null);
			}
		}catch (Exception e) {
			e.printStackTrace();
			return SysResult.build(202, "错误请重试", null);
		}	
	}
	//换车(需要参数carId orderId)
	@RequestMapping("transfer")
	public SysResult transfer(Order order) {
		try {
			peanutService.updateorder(order);
			return SysResult.ok();
		}catch (Exception e) {
			e.printStackTrace();
			return SysResult.build(201, "换车失败", null);
		}
	
	}
	/**
	 * 租车系统
	 * @param order 
	 * @return SysResult 数据信息
	 */
	@RequestMapping("rent")
	public SysResult rentCar(Order order,Integer time,String money){
		try {
			peanutService.zuulCar(order,time,money);
			//System.out.println(car);
			return SysResult.build(200,"租车成功",null);
			//return SysResult.ok();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return SysResult.build(201,"抱歉，本次租车未成功",null);
		}
	}
 }
	

